package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.service.ReportService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    private ReportService reportService;

    @RequestMapping("/getMemberAgeReport.do")
    public Result getMemberAgeReport(){
        return reportService.getMemberAgeReport();
    }

    @RequestMapping("/getMemberSexReport.do")
    public Result getMemberSexReport(){
        return reportService.getMemberSexReport();
    }

    @RequestMapping("/getMemberReport.do")
    public Result getMemberReport(){
        return reportService.getMemberReport();
    }

    @RequestMapping("/findMemberByDate.do")
    public Result findMemberByDate(@RequestParam("startDate") String startDate,@RequestParam("endDate") String endDate) throws ParseException {
        return reportService.findMemberByDate(startDate,endDate);
    }

    @RequestMapping("/findReportBusiness.do")
    public Result findReportBusiness(){
        return reportService.findReportBusiness();
    }

    @RequestMapping("/exportExcel.do")
    public void exportExcel(HttpServletResponse response, HttpSession session){
        try {
            /*//获取Excel模板的绝对路径
            String templatePath = session.getServletContext().getRealPath("template")+ File.separator+"report_template";

            //读取模板获取Excel对象
            XSSFWorkbook workbook = new XSSFWorkbook(templatePath);*/
            ServletContext servletContext = session.getServletContext();

            String realPath = servletContext.getRealPath("/template/report_template.xlsx");
            File file = new File(realPath);
            FileInputStream fileInputStream = new FileInputStream(file);
            XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);

            //给sheet单元格赋值
            //获取运营表格中的数据的数据
            Map<String,Object> data = (Map<String, Object>) reportService.findReportBusiness().getData();
            //System.out.println("数据"+data);

            //获取工作表
            XSSFSheet sheet = workbook.getSheetAt(0);

            //取出返回结果数据，准备将报表数据写入到Excel文件中
            String reportDate = (String) data.get("reportDate");
            sheet.getRow(2).getCell(5).setCellValue(reportDate);

            Integer todayNewMember = (Integer) data.get("todayNewMember");
            sheet.getRow(4).getCell(5).setCellValue(todayNewMember);

            Integer totalMember = (Integer) data.get("totalMember");
            sheet.getRow(4).getCell(7).setCellValue(totalMember);

            Integer thisWeekNewMember = (Integer) data.get("thisWeekNewMember");
            sheet.getRow(5).getCell(5).setCellValue(thisWeekNewMember);

            Integer thisMonthNewMember = (Integer) data.get("thisMonthNewMember");
            sheet.getRow(5).getCell(7).setCellValue(thisMonthNewMember);

            Integer todayOrderNumber = (Integer) data.get("todayOrderNumber");
            sheet.getRow(7).getCell(5).setCellValue(todayOrderNumber);

            Integer thisWeekOrderNumber = (Integer) data.get("thisWeekOrderNumber");
            sheet.getRow(8).getCell(5).setCellValue(thisWeekOrderNumber);

            Integer thisMonthOrderNumber = (Integer) data.get("thisMonthOrderNumber");
            sheet.getRow(9).getCell(5).setCellValue(thisMonthOrderNumber);

            Integer todayVisitsNumber = (Integer) data.get("todayVisitsNumber");
            sheet.getRow(7).getCell(7).setCellValue(todayVisitsNumber);

            Integer thisWeekVisitsNumber = (Integer) data.get("thisWeekVisitsNumber");
            sheet.getRow(8).getCell(7).setCellValue(thisWeekVisitsNumber);

            Integer thisMonthVisitsNumber = (Integer) data.get("thisMonthVisitsNumber");
            sheet.getRow(9).getCell(7).setCellValue(thisMonthVisitsNumber);

            List<Map> hotSetmeal = (List<Map>) data.get("hotSetmeals");
            //遍历集合
            int index = 12;
            for (Map map : hotSetmeal) {
                String name = (String) map.get("name");//套餐名称
                Long setmeal_count = (Long) map.get("setmeal_count");//预约数量
                BigDecimal proportion = (BigDecimal) map.get("proportion");//占比
                String remark = (String) map.get("remark");
                XSSFRow row = sheet.getRow(index);
                row.getCell(4).setCellValue(name);
                row.getCell(5).setCellValue(setmeal_count);
                row.getCell(6).setCellValue(proportion.toString());
                row.getCell(7).setCellValue(remark);
                index++;
            }

            //设置响应文件格式
            response.setContentType("application/vnd.ms-excel");
            //设置下载方式
            response.setHeader("content-Disposition","attachment;filename=report.xlsx");
            //获取输出流
            ServletOutputStream out = response.getOutputStream();

            //将excel响应出去
            workbook.write(out);
            out.flush();//清空缓冲区
            out.close();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

        }
    }

    @RequestMapping("/getSetmealReport.do")
    public Result getSetmealReport(){
        return reportService.getSetmealReport();
    }
}
